<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="renderer" content="webkit">
<title></title>
<link rel="stylesheet" href="admin/css/pintuer.css">
<link rel="stylesheet" href="admin/css/admin.css">
<script src="admin/js/jquery.js"></script>
<script src="admin/js/pintuer.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	
	$("#url2").change(function(){
		//图片预览方法
		var objUrl = getObjectURL(this.files[0]) ;
// 		console.log("objUrl = "+objUrl) ;
		if (objUrl) {
			$("#showImg").attr("src", objUrl) ;
		}
	});
	$('#uadv_form').bind('submit',function(){
		var formData = new FormData();
		var file = $("#url2")[0].files[0];
		var id = $('input[name="id"]').val();
		var addorupdate = $('input[name="addOrUpdate"]').val();
		var title = $('input[name="title"]').val();
		var content = $('textarea[name="content"]').val();
		if(file != null){
			formData.append('img',file);
		}
		formData.append('title',title);
		formData.append('content',content);
		if(id != '0'){
			formData.append('id',id);
		}
		if(addorupdate != '0'){
			formData.append('addorupdate',addorupdate);
		}
		$.ajax({
			url:'admin/addAdvInfo.do',
			type:'post',
			data:formData,
			dataType:'html',
			cache: false,  
	        contentType: false,  
	        processData: false,
			success:function(strValue){
				if(strValue.length > 0){
					alert(strValue);
					clearData();
					window.location.href="admin/adv.do";
				}
			}
		});
		return false;
		
});
});
//建立一个可存取到该文件的URL
function getObjectURL(file) {
	var url = null ; 
	if (window.createObjectURL!=undefined) { // basic
		url = window.createObjectURL(file) ;
	} else if (window.URL!=undefined) { // mozilla(firefox)
		url = window.URL.createObjectURL(file) ;
	} else if (window.webkitURL!=undefined) { // webkit or chrome
		url = window.webkitURL.createObjectURL(file) ;
	}
	return url ;
}
//删除轮播图
function del(id){
	if(confirm("确定删除id为"+id+"的轮播图？")){
		$.ajax({url:'admin/delAdv.do?id='+id,context: document.body,success:function(data){
			alert(data);
			window.location.reload();
		}})
	}
}

function fillcontext(id,title,resource,content){
	$('#addorupdate').html('修改内容');
	$('input[name="id"]').val(id);
	$('input[name="addOrUpdate"]').val('1');
	$('input[name="title"]').val(title);
	$('textarea[name="content"]').val(content);
	$('#showImg').attr('src',resource);
}
function clearData(){
	document.getElementById('add').scrollIntoView();
	$('#addorupdate').html('增加内容');
	$('input[name="id"]').val('');
	$('input[name="addOrUpdate"]').val('');
	$('input[name="title"]').val('');
	$('textarea[name="content"]').val('');
	$('#showImg').attr('src','');
}
</script> 
</head>
<body>
<div class="panel admin-panel">
  <div class="panel-head"><strong class="icon-reorder"> 内容列表</strong></div>
  <div class="padding border-bottom">  
  <button type="button" class="button border-yellow" onclick="document.getElementById('add').scrollIntoView();"><span class="icon-plus-square-o"></span> 添加内容</button>
  </div>
  <table class="table table-hover text-center">
    <tr>
      <th width="10%">ID</th>
      <th width="20%">图片</th>
      <th width="15%">名称</th>
      <th width="20%">描述</th>
      <th width="10%">排序</th>
      <th width="15%">操作</th>
    </tr>
   	<c:if test="${!empty advs }">
   		<c:forEach items="${advs }" var="adv" varStatus="a">
   			<tr>
      			<td>${adv.id }</td>     
      			<td><img src="${adv.resource }" alt="${adv.title }" width="120" height="50" /></td>     
      			<td>${adv.title }</td>
      			<td>${adv.content }</td>
      			<td>${a.count }</td>
      			<td><div class="button-group">
      			<a class="button border-main" href="javascript:void(0)" onclick="fillcontext('${adv.id}','${adv.title }','${adv.resource }','${adv.content }')"><span class="icon-edit"></span> 修改</a>
      			<a class="button border-red" href="javascript:void(0)" onclick="del(${adv.id})"><span class="icon-trash-o"></span> 删除</a>
      			</div></td>
    		</tr>
   		</c:forEach>
   	</c:if>
    
  </table>
</div>
<div class="panel admin-panel margin-top" id="add">
  <div class="panel-head"><strong><span class="icon-pencil-square-o"></span> <b id="addorupdate">增加内容</b></strong></div>
  <div class="body-content">
    <form id="uadv_form" method="post" class="form-x" enctype="multipart/form-data"> 
    	<input type="hidden" name="id" value="0"/> 
    	<input type="hidden" name="addOrUpdate" value="0"/>   
      <div class="form-group">
        <div class="label">
          <label>名称：</label>
        </div>
        <div class="field">
          <input type="text" class="input w50" name="title" data-validate="required:请输入名称" />
          <div class="tips"></div>
        </div>
      </div>
      
      <div class="form-group">
        <div class="label">
          <label>图片：</label>
        </div>
        <div class="field">
          <input type="file" id="url2" name="img" class="input tips" style="width:25%; float:left;"  data-place="right" />
          <div class="tipss">图片尺寸：1920*500</div>
        </div>
      </div>
      <div class="form-group">
        <div class="label">
          <label>图片预览：</label>
        </div>
<!--         <div class="tip"> -->
        	<img id="showImg" src="" width="240" height="100"/>
<!--         </div> -->
      </div>
      <div class="form-group">
        <div class="label">
          <label>描述：</label>
        </div>
        <div class="field">
          <textarea type="text" class="input" name="content" style="height:120px;"></textarea>
          <div class="tips"></div>
        </div>
      </div>
      <c:if test="${! empty addRslt }">
      <div id="uresultdiv" class="form-group" style="display: none;">
        <div class="label">
          <label for="sitename">更新结果：</label>
        </div>
        <div class="label" style="width: 300px;">
          <div id="uresult" style="color:red;">${addRslt }</div>
        </div>
      </div>
      </c:if>
      <div class="form-group">
        <div class="label">
          <label></label>
        </div>
        <div class="field">
          <input class="button bg-main icon-check-square-o" type="submit" value="提交"/>
        </div>
      </div>
    </form>
  </div>
</div>
</body></html>